package WidgetFun

import (
	"clientDemo/ui/fyne_ui/widget/interface"
	"strings"
)

func GetChildWidget(node widgetcommon.IWidgetItem, name string) widgetcommon.IWidgetItem {
	if node == nil {
		return nil
	}
	// 分割name 用“：”
	arr := strings.Split(name, ":")
	if len(arr) >= 2 {
		return GetChildWidgetSkin(node, arr[0], arr[1])
	}
	return GetChildWidgetByName(node, name)
}
func GetChildWidgetByName(node widgetcommon.IWidgetItem, name string) widgetcommon.IWidgetItem {
	if node == nil {
		return nil
	}
	info := GetUserInfo(node)
	if info == nil {
		return nil
	}
	if info.GetName() == name {
		return node
	}
	c, ok := node.(widgetcommon.IWidgetItem)
	if !ok {
		return nil
	}
	var result widgetcommon.IWidgetItem
	// 优先选择一级子节点
	c.ForEach(func(child widgetcommon.IWidgetItem) bool {
		info := GetUserInfo(child)
		if info == nil {
			return false
		}
		if info.GetName() == name {
			result = child
			return true
		}
		return false
	})
	// 再选多级子节点
	c.ForEach(func(child widgetcommon.IWidgetItem) bool {
		obj := GetChildWidgetByName(child, name)
		if obj != nil {
			result = obj
			return true
		}
		return false
	})
	return result
}
func GetChildWidgetSkin(node widgetcommon.IWidgetItem, sceneName string, name string) widgetcommon.IWidgetItem {
	if node == nil {
		return nil
	}
	sceneNode := GetChildWidgetByName(node, sceneName)
	if sceneNode == nil {
		return nil
	}
	return GetChildWidgetByName(sceneNode, name)
}
func GetUserInfo(node widgetcommon.IWidgetItem) widgetcommon.IUserInfo {
	if node == nil {
		return nil
	}
	return node.GetUserInfo()
}
